﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace TimeReporterWF
{
    class oldTimeSlipDataSet : TimeSlipDataSetTyped
    {
        
        // Constructor
        public oldTimeSlipDataSet() : base()
        {
            //this.Clear();
            //this.Tables.Add(new TimeSlipDataTable());
        }

        // Load from files
        public void Load(DirectoryInfo theFolder)
        {
            
            FileInfo[] theFiles = theFolder.GetFiles("*.csv");
            foreach (FileInfo fi in theFiles)
            {
                System.Console.WriteLine(fi.FullName.ToString());

                this.Merge(GetData(fi));

            }            
        }
        public void GetData2(FileInfo theFile)
        {
            
        }


        // Read data from a file
        private static DataSet GetData(FileInfo theFile)
        {
            string strLine;         
            string[] strArray;
            char[] charArray = new char[] { ',' };
            DataSet tmpDataSet = new DataSet();
            

            FileStream aFile = new FileStream(theFile.FullName.ToString(), FileMode.Open);
            StreamReader sr = new StreamReader(aFile);


            //Read past header line
            strLine = sr.ReadLine();
            /*
            This code can be used to automatically set up table columns
            but I don't need it here
            strArray = strLine.Split(charArray);

            for (int x = 0; x <= strArray.GetUpperBound(0); x++)
            {
                dt.Columns.Add(strArray[x].Trim());
            }
            */
            tmpDataSet.Tables.Add(new TimeSlipDataTable());
            
            strLine = sr.ReadLine();
            while (strLine != null)
            {
                strArray = strLine.Split(charArray);

                

                DataRow dr = tmpDataSet.Tables[0].NewRow();

                dr["Project"] = theFile.Name.ToString();
                dr["TaskId"] = strArray[0];
                dr["TimeSpent"] = (float)Convert.ToDouble(strArray[1]);
                dr["UserId"] = strArray[2];
                dr["EndDateTime"] = Convert.ToDateTime(strArray[3]);
                dr["StartDateTime"] = Convert.ToDateTime(strArray[4]);

                //dr.Field("TaskId") = strArray[0];

                /*
                for (int i = 0; i <= strArray.GetUpperBound(0); i++)
                {

                    if (i == 3 | i == 4)
                    {
                        DateTime x = Convert.ToDateTime(strArray[i]);
                        Console.WriteLine("Year: {0}, Month: {1}, Day: {2}", x.Year, x.Month, x.Day);
                    }
                    dr[i] = strArray[i].Trim();
                }
                 */

                tmpDataSet.Tables[0].Rows.Add(dr);
                strLine = sr.ReadLine();
            }
            sr.Close();
            return tmpDataSet;

        }
    }
}
